package com.dwyinyuan.application;

import com.dwyinyuan.common.ResourceListener;
import com.dwyinyuan.consumer.Consumer;
import com.dwyinyuan.enums.ProviderEnums;
import com.dwyinyuan.factory.SpiderFactory;
import com.dwyinyuan.producer.*;

import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;

/**
 * 微信公众号：笛舞音缘
 * 入口
 */
public class SpiderApplication {

    public static void main(String[] args) throws IOException {
        ResourceListener.addListener("D:\\practice\\spider-img");//观察者模式

        BlockingQueue<String> blockingQueue = new SynchronousQueue<String>(true);//队列 1
        ExecutorService proservice = Executors.newSingleThreadExecutor();//生产者线程
        //ExecutorService conservice = Executors.newSingleThreadExecutor();//消费者线程
        ExecutorService conservice =Executors.newFixedThreadPool(5);

        ExecutorService taskPool = Executors.newCachedThreadPool();

        String providerName = "MeiziProducer2";
        proservice.submit(new MeiziProducer2("Producer", blockingQueue));//V1

//        submitProvider(providerName, proservice, blockingQueue);//V2




//        SpiderFactory spiderFactory = new SpiderFactory();
//        Runnable producer = spiderFactory.getProducer(providerName, blockingQueue);
//        proservice.submit(producer);//V3 工厂


//        proservice.submit(ProviderEnums.getTaskByName(providerName, blockingQueue));//V4


        conservice.submit(new Consumer("Consumer", blockingQueue, taskPool));
        //proservice.shutdown();
        //conservice.shutdown();
    }

    public static void submitProvider(String providerType, ExecutorService proservice,
                                      BlockingQueue<String> blockingQueue) {
        if (ProviderEnums.FENG_NIAO.getName().equals(providerType)) {
            proservice.submit(new FengniaoProducer("Producer", blockingQueue));
        } else if (ProviderEnums.HUA_SHI_6.getName().equals(providerType)) {
            proservice.submit(new Huashi6Producer("Producer", blockingQueue));
        } else if (ProviderEnums.MEI_NV_LA.getName().equals(providerType)) {
            proservice.submit(new MeinvlaProducer("Producer", blockingQueue));
        } else if (ProviderEnums.TANG_YUN_365.getName().equals(providerType)) {
            proservice.submit(new Tangyun365Producer("Producer", blockingQueue));
        } else if (ProviderEnums.WALL_HAVEN.getName().equals(providerType)) {
            proservice.submit(new WallHavenProducer("Producer", blockingQueue));
        } else if (ProviderEnums.WIN_4000.getName().equals(providerType)) {
            proservice.submit(new Win4000Producer("Producer", blockingQueue));
        } else {
            throw new RuntimeException("未找到对应的生产者。。。");
        }
    }

}
